<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cdn-animation</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/lottie-web/5.7.8/lottie.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/16.7.0/Tween.js"></script>
</head>
<style>
    html,
    body {
        width: 100vw;
        width: 100vh;
    }

    .bodymovin {
        width: 200px;
        height: 200px;
        position: absolute;
        transform: translate(-30%, 0);
        top: 50px;
        left: 0;

    }

    .feces {
        z-index: 1;
        position: absolute;
        background-image: url(./feces.png);
        background-size: 100%;
        background-position: center;
        background-repeat: no-repeat;
        width: 80px;
        height: 80px;
        transform-origin: center;
    }
    /* .hide {
        animation: hide 0.5s;
        animation
    }
    @keyframes hide {
        from {
            opacity: 1;
        }
        to {
            opacity: 0;
        }
    } */

</style>

<body>
    <div class="bodymovin"></div>

    <script>
        const center = 150;
        const length = 80;
        function animate(time) {
            requestAnimationFrame(animate);
            TWEEN.update(time);
        }
        requestAnimationFrame(animate);

        function createfeces(scale) {
            const fece = document.createElement('div');
            fece.className = 'feces';
            const symbol = Math.random() > 0.5 ? 1 : -1;
            fece.style.transform = `scale(${scale}) rotate(${symbol * 20 * Math.random()}deg)`
            fece.style.opacity = '0';
            return fece;
        }

        function initFece(end) {
            const dis = Math.sqrt((end.x - 150) * (end.x - 150) + (end.y - 150) * (end.y - 150));
            const r = Math.pow(2 / 3, dis / length);
            const fece = createfeces(r);
            document.body.appendChild(fece);

            const start = { x: 0, y: 100, z: 0 };
            const tween = new TWEEN.Tween(start)
                .to({ ...end, z: 1 }, 100)
                .easing(TWEEN.Easing.Linear.None)
                .onUpdate(function () {
                    fece.style.setProperty('top', `${start.y}px`);
                    fece.style.setProperty('left', `${start.x}px`);
                    fece.style.setProperty('opacity', `${start.z}`);
                })
                .onComplete(function () {
                    initDown(start, fece).start();
                })
            return tween;
        }
        function initDown(start, fece) {
            const s = {
                y: start.y,
                o: 1,
            };
            const e = { y: start.y + 80, o: 0 };
            const tween = new TWEEN.Tween(s)
                .to(e, 2000 + 500 * Math.random())
                .easing(TWEEN.Easing.Quadratic.In)
                .onUpdate(function () {
                    fece.style.setProperty('top', `${s.y}px`);
                    fece.style.setProperty('opacity', `${s.o}`);
                })
                .onComplete(function () {


                })
            return tween;
        }

        function randomPosition(num) {
            const randomX = Math.random() * 2 * length - length;
            const y = Math.round(Math.sqrt(length * length - randomX * randomX));
            const radian = Math.atan(y / randomX);

            const step = Math.PI * 2 / num;

            return new Array(num).fill(0).map((item, index) => {
                const r = (index * step + radian)
                const tr = r > Math.PI * 2 ? r - Math.PI * 2 : r < 0 ? r + Math.PI * 2 : r;
                return {
                    x: length * Math.sin(tr) + (Math.random() > 0.5 ? 1 : -1) * 10 * Math.random(),
                    y: length * Math.cos(tr) + (Math.random() > 0.5 ? 1 : -1) * 10 * Math.random(),
                }
            })
        }

        fetch('./bomb.json')
            .then(data => {
                renderBomb(() => {
                    const animation = window.bodymovin.loadAnimation({
                        container: document.querySelector('.bodymovin'), // 要包含该动画的dom元素
                        renderer: 'svg', // 渲染方式，svg、canvas、html（轻量版仅svg渲染）
                        loop: false, // 是否循环播放
                        autoplay: false, // 是否自动播放
                        path: './bomb.json', // 动画json文件路径
                    });
                    animation.play();
                    [
                        { x: 150 - 15 * Math.random(), y: 150 - 15 * Math.random() },
                        // 这里的 6 可以改成很大的数字，就会有很多粑粑
                        ...randomPosition(6).map(item => ({ x: item.x + 150, y: item.y + 150 }))
                    ].forEach(item => {
                        initFece(item).start();
                    })
                    animation.addEventListener('complete', () => {

                        animation.destroy()
                    })
                })
            })
        function renderBomb(cb) {
            var box = document.createElement('div');
            box.style.setProperty('width', '20px');
            box.style.setProperty('height', '20px');
            box.style.setProperty('background-position', 'center');
            box.style.setProperty('background-size', '100%');
            box.style.setProperty('background-image', 'url(./bomb.png)');
            box.style.setProperty('transform', 'translate(' + 300 + 'px, ' + 300 + 'px)');
            document.body.appendChild(box);

            function animate(time) {
                requestAnimationFrame(animate);
                TWEEN.update(time);
            }
            requestAnimationFrame(animate);

            var H = 100;

            var coords = { x: 300, y: 0 };
            var tween = new TWEEN.Tween(coords)
                .to({ x: 0, y: 360 }, 1000)
                .easing(TWEEN.Easing.Linear.None)
                .onUpdate(function () {
                    var c = H;
                    var a = 5 / (6 * H);
                    var b = -11 / 6;
                    var x = coords.x;
                    var y = a * x * x + b * x + c;
                    box.style.setProperty('transform', `translate(${x}px, ${y}px) rotate(${coords.y}deg)`);
                })
                .start()
                .onComplete(function () {
                    box.style.setProperty('display', 'none');
                    cb && cb()
                })
        }

    </script>
</body>

</html>